package com.chinacoal.hr.levelimpl.dao;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chinacoal.hr.levelapi.entity.AttendanceInfo;
import com.chinacoal.hr.levelimpl.mapper.AttendanceInfoMapper;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author: YunTao.Li
 * @create: 2019-11-21 00:16
 * @description:
 **/
@Component
public class AttendanceInfoDao {

    @Resource
    private AttendanceInfoMapper attendanceInfoMapper;


    /**
     * @param condition :
     * @return : com.baomidou.mybatisplus.core.metadata.IPage<com.chinacoal.hr.levelimpl.entity.AttendanceInfo>
     * @author : YunTao.Li
     * @date : 2019/11/21 2019/11/21
     */
    public IPage<AttendanceInfo> getAttendanceInfoPage(Map<String, Object> condition) {
        Long currentPage = (Long) condition.get("page");
        Long currentPageSize = (Long) condition.get("limit");
        //Long currentPageSize = Long.MAX_VALUE;

        String ouId = condition.get("ouId") != null ? condition.get("ouId").toString() : "";
        String orgCode = condition.get("orgCode") != null ? condition.get("orgCode").toString() : "";
        String deptCode = condition.get("deptCode") != null ? condition.get("deptCode").toString() : "";
        String deptName = condition.get("deptName") != null ? condition.get("deptName").toString() : "";
        String empCode = condition.get("empCode") != null ? condition.get("empCode").toString() : "";
        String beginDate = condition.get("beginDate") != null ? condition.get("beginDate").toString() : "";
        String endDate = condition.get("endDate") != null ? condition.get("endDate").toString() : "";
        String empType = condition.get("empType") != null ? condition.get("empType").toString() : "";
        String signUpStatus = condition.get("signUpStatus") != null ? condition.get("signUpStatus").toString() : "";
        String empId = condition.get("empId") != null ? condition.get("empId").toString() : "";
        String confirmStatus = condition.get("confirmStatus") != null ? condition.get("confirmStatus").toString() : "";
        String yrmonth = condition.get("yrmonth") != null ? condition.get("yrmonth").toString() : "";
        String queryRange = condition.get("queryRange") != null ? condition.get("queryRange").toString() : "";

        IPage<AttendanceInfo> pageObject = new Page(currentPage, currentPageSize);


        // 开始加入查询条件，为空的值将不会被拼接到查询条件中
        QueryWrapper queryWrapper = new QueryWrapper();

        if (StringUtils.isNotBlank(orgCode)) {
            queryWrapper.eq("ORG_CODE", orgCode);
        }

        if (StringUtils.isNotBlank(deptName)) {
            queryWrapper.eq("DEPT_NAME", deptName);
        }

        if (StringUtils.isNotBlank(empId)) {
            queryWrapper.eq("EMP_ID", empId);
        }

        if (StringUtils.isNotBlank(empCode)) {
            queryWrapper.eq("EMP_CODE", empCode);
        }

        if (StringUtils.isNotBlank(beginDate)) {
            queryWrapper.apply("UNIX_TIMESTAMP(ATTEND_DATE) >= UNIX_TIMESTAMP('" + beginDate + "')");
        }

        if (StringUtils.isNotBlank(endDate)) {
            queryWrapper.apply("UNIX_TIMESTAMP(ATTEND_DATE) <= UNIX_TIMESTAMP('" + endDate + "')");
        }

        if (StringUtils.isNotBlank(confirmStatus)) {
            queryWrapper.eq("CONFIRM_STATUS", confirmStatus);
        }

        if (StringUtils.isNotBlank(yrmonth)) {
            queryWrapper.apply("date_format(ATTEND_DATE,'%Y-%m') = '" + yrmonth + "'");
        }

        // 查询范围
        // TODO:
        if (StringUtils.isNotBlank(queryRange)) {
            if (("all").equals(queryRange)) {

            } else if (("dept").equals(queryRange)) {
                if (StringUtils.isNotBlank(deptCode)) {
                    queryWrapper.eq("DEPT_CODE", deptCode); //业务二处
                }
            } else if (("person").equals(queryRange)) {
                if (StringUtils.isNotBlank(empCode)) {
                    queryWrapper.eq("EMP_CODE", empCode); //刘爽
                }
            }
        }

        queryWrapper.orderByDesc("ATTEND_DATE");
        queryWrapper.orderByAsc("ORG_CODE");
        queryWrapper.orderByDesc("DEPT_CODE");


        IPage<AttendanceInfo> attendanceInfoIPage = attendanceInfoMapper.selectPage(pageObject, queryWrapper);

        return attendanceInfoIPage;
    }

    /**
     * 获得考勤信息明细记录
     *
     * @param condition :
     * @return : java.util.List<com.chinacoal.hr.levelimpl.entity.AttendanceInfo>
     * @author : YunTao.Li
     * @date : 2020/7/6 2020/7/6
     */
    public List<AttendanceInfo> getAttendanceInfoList(Map<String, Object> condition) {
        String beginDate = condition.get("beginDate") != null ? condition.get("beginDate").toString() : "";
        String endDate = condition.get("endDate") != null ? condition.get("endDate").toString() : "";

        QueryWrapper queryWrapper = new QueryWrapper();

        if (StringUtils.isNotBlank(beginDate)) {
            queryWrapper.apply("UNIX_TIMESTAMP(ATTEND_DATE) >= UNIX_TIMESTAMP('" + beginDate + "')");
        }

        if (StringUtils.isNotBlank(endDate)) {
            queryWrapper.apply("UNIX_TIMESTAMP(ATTEND_DATE) <= UNIX_TIMESTAMP('" + endDate + "')");
        }

        queryWrapper.orderByAsc("EMP_CODE");
        queryWrapper.orderByAsc("ATTEND_DATE");


        List<AttendanceInfo> attendanceInfoList = attendanceInfoMapper.selectList(queryWrapper);

        return attendanceInfoList;
    }

    public void insert(AttendanceInfo attendanceInfo) throws Throwable {
        this.attendanceInfoMapper.insert(attendanceInfo);
    }

    /**
     * 删除考勤信息
     *
     * @param condition :
     * @return : void
     * @author : YunTao.Li
     * @date : 2019/12/26 2019/12/26
     */
    public void delete(Map condition) throws Throwable {
        String empCode = condition.get("empCode") != null ? condition.get("empCode").toString() : "";
        Date attendDate = condition.get("attendDate") != null ? (Date) condition.get("attendDate") : null;

        QueryWrapper queryWrapper = new QueryWrapper();
        if (StringUtils.isNotBlank(empCode)) {
            queryWrapper.eq("EMP_CODE", empCode);
        }

        if (attendDate != null) {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            String attendDateStr = format.format(attendDate);

            queryWrapper.apply("date_format(ATTEND_DATE,'%Y-%m-%d') = '" + attendDateStr + "'");
        }
        this.attendanceInfoMapper.delete(queryWrapper);
    }

    /**
     * 获得考勤信息明细记录(select one)
     *
     * @param condition :
     *                  attendDate:yyyy-MM-dd
     * @return : com.chinacoal.hr.levelimpl.entity.AttendanceInfo
     * @author : YunTao.Li
     * @date : 2020/7/24 2020/7/24
     */
    public AttendanceInfo getAttendanceInfo(Map condition) {
        String empCode = condition.get("empCode") != null ? condition.get("empCode").toString() : "";
        String attendDate = condition.get("attendDate") != null ? condition.get("attendDate").toString() : "";
        QueryWrapper queryWrapper = new QueryWrapper();

        if (StringUtils.isNotBlank(empCode)) {
            queryWrapper.eq("EMP_CODE", empCode);
        }

        if (StringUtils.isNotBlank(attendDate)) {
            String sql = "UNIX_TIMESTAMP('" + attendDate + " 00:00:00') <= UNIX_TIMESTAMP(ATTEND_DATE)" +
                    "and UNIX_TIMESTAMP('" + attendDate + " 23:59:59') >= UNIX_TIMESTAMP(ATTEND_DATE)";
            queryWrapper.apply(sql);
        }

        queryWrapper.last("limit 1");

        return this.attendanceInfoMapper.selectOne(queryWrapper);
    }

    /**
     * 修改考勤明细记录
     *
     * @param attendanceInfo :
     * @return : void
     * @author : YunTao.Li
     * @date : 2020/7/24 2020/7/24
     */
    public void updateAttendanceInfo(AttendanceInfo attendanceInfo) {
        this.attendanceInfoMapper.updateById(attendanceInfo);
    }
}
